package com.theme.common.third.applepay;

/**
 * @author: 可乐加糖
 * @create: 2023-02-21 15:58
 * @Description: https://blog.csdn.net/m0_62949970/article/details/124570436
 */
public class ApplePayHandler {

    /**
     * TransactionID：苹果配置的产品id
     * String receipt_data:需要客户端传过来的参数2
     * 苹果内购支付
     *
     * @Title: doIosRequest
     * @Description:Ios客户端内购支付
     */
//    @RequestMapping("/applePay")
//    public Result doIosRequest(HttpServletRequest request, @RequestBody ApplePayVo applePayVo) {
//        Result ext = new Result();
//        String receipt_data = applePayVo.getReceipt_data();
//        String transactionID = applePayVo.getTransaction_id();
//        String verifyResult = iosVerifyUtil.buyAppVerify(receipt_data,applePayVo.getType());
//        //苹果服务器没有返回验证结果
//        if (verifyResult == null) {
//            JSONObject jsonObject= new JSONObject();
//            jsonObject.put("verify_status",-1);
//            ext.setData(jsonObject);
//            ext.setMessage("无订单信息!");
//            ext.setCode(-1);
//            return ext;
//        } else {
//            // 苹果验证有返回结果
//            //log.info("线上，苹果平台返回JSON:" + verifyResult);
//            JSONObject job = JSONObject.parseObject(verifyResult);
//            String states = job.getString("status");
//            //获取苹果系统服务器验证结果数据处理自己的业务
//            Result appPay = applePayService.getAppPay(states, job, transactionID, request, applePayVo);
//            return appPay;
//        }
//    }

    /**
     * 苹果支付验证后
     **/
//    public Result getAppPay(String states, JSONObject job, String transactionID, HttpServletRequest request, ApplePayVo applePayVo) {
//        Result ext = new Result();
//        //log.info("苹果平台返回值：job" + job);
//        //判断是否验证成功
//        if ("0".equals(states)) {
//            //app端所提供的收据是有效的    验证成功
//            String r_receipt = job.getString("receipt");
//            JSONObject returnJson = JSONObject.parseObject(r_receipt);
//            String in_app = returnJson.getString("in_app");
//            JSONObject in_appJson = JSONObject.parseObject(in_app.substring(1, in_app.length() - 1));
//            String transactionId = in_appJson.getString("transaction_id");
//            //产品id
//            String productId = in_appJson.getString("product_id");
//            //苹果支付订单状态
//            String in_app_ownership_type = in_appJson.getString("in_app_ownership_type");
//            //如果获取验证后的支付订单单号与app端传来的支付订单号一致并且状态为已支付状态则处理自己的业务
//            if (transactionID.equals(transactionId) && in_app_ownership_type.equals("PURCHASED")) {
//            ===================处理自己的业务============================
//
//            }
//        }
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("verify_status", -1);
//        ext.setMessage("receipt数据有问题");
//        ext.setData(jsonObject);
//        ext.setCode(-1);
//        return ext;
    }


//    新版IOS返回（7.0以后）
//    {
//        "receipt": {
//        "receipt_type": "ProductionSandbox",
//                "adam_id": 0,
//                "app_item_id": 0,
//                "bundle_id": "com.xxxx.xxxx",
//                "application_version": "1",
//                "download_id": 0,
//                "version_external_identifier": 0,
//                "receipt_creation_date": "2021-11-01 09:20:51 Etc/GMT",
//                "receipt_creation_date_ms": "1635758451000",
//                "receipt_creation_date_pst": "2021-11-01 02:20:51 America/Los_Angeles",
//                "request_date": "2021-11-01 09:20:52 Etc/GMT",
//                "request_date_ms": "1635758452973",
//                "request_date_pst": "2021-11-01 02:20:52 America/Los_Angeles",
//                "original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
//                "original_purchase_date_ms": "1375340400000",
//                "original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
//                "original_application_version": "1.0",
//                "in_app": [{
//            "quantity": "1",
//                    "product_id": "",//唯一的
//                    "transaction_id": "1000000901786189",
//                    "original_transaction_id": "1000000901786189",
//                    "purchase_date": "2021-11-01 09:13:33 Etc/GMT",
//                    "purchase_date_ms": "1635758013000",
//                    "purchase_date_pst": "2021-11-01 02:13:33 America/Los_Angeles",
//                    "original_purchase_date": "2021-11-01 09:13:33 Etc/GMT",
//                    "original_purchase_date_ms": "1635758013000",
//                    "original_purchase_date_pst": "2021-11-01 02:13:33 America/Los_Angeles",
//                    "is_trial_period": "false",
//                    "in_app_ownership_type": "PURCHASED"
//        }]
//    },
//        "environment": "Sandbox",
//            "status": 0
//    }
